#include "settingparser.h"
#include <QDebug>
SettingParser* SettingParser::parser=nullptr;
//QMap<QString,std::function<void(bool)>> SettingParser::changefuncmap=QMap<QString,std::function<void(bool)>>();


SettingParser *SettingParser::getInstance()
{
    if(parser==nullptr)
    {
        parser=new SettingParser();
    }
    return parser;
}

void SettingParser::initParser(const QString &path)
{
    changefuncmap["newmessagering"] = [this](bool status){ emit s_newmessagering(status); };
    changefuncmap["voicecallring"]=[this](bool status){ emit s_voicecallring(status); };
    changefuncmap["videocallring"]=[this](bool status){ emit s_videocallring(status); };
    changefuncmap["enter"]=[this](bool status){ emit s_enter(status); };
    changefuncmap["ring"]=[this](bool status){ emit s_ring(status); };

    changed=false;
    filepath=path;
    QFile file(path);
    if(!file.exists())
    {
        file.open(QIODevice::ReadWrite);
        arr=newSettings();
        file.write(Encipher::getInstance()->aesEncrypt(arr));
        changed=true;

    }
    else
    {
        file.open(QIODevice::ReadOnly);
        arr=Encipher::getInstance()->aesDecrypt(file.readAll());
    }
    analysisFile();
    qDebug()<<"一开始 "<<settingmap;
    file.close();
    emit s_loadDone();
}



void SettingParser::analysisFile()
{
    QJsonParseError error;
    QJsonDocument doc=QJsonDocument::fromJson(arr,&error);
    if (error.error != QJsonParseError::NoError)
    {
        qWarning("Failed to parse JSON data");
        return;
    }
    QJsonObject rootobj=doc.object();

    for(auto key:rootobj.keys())
    {
        QJsonValue value = rootobj[key];
        if (value.isObject())
        {
            // 如果是对象，递归遍历
            QJsonObject subObject = value.toObject();
            for (auto subKey : subObject.keys())
            {
                settingmap.insert(subKey,subObject.value(subKey).toString());
            }
        }
        else
        {
            continue;
        }

    }
}

QString SettingParser::getValue(const QString &key)
{

    if(settingmap.contains(key))
    {
        //qDebug()<<"get return "<< key<<settingmap[key];
        return settingmap[key];
    }
    else
        return QString();

}

void SettingParser::setValue(const QString &key, const QString &value)
{

    if(settingmap.contains(key))
    {
        changed=true;
        settingmap[key]=value;
        qDebug()<<key<<"  --  "<<value;
        if(changefuncmap.contains(key))
            changefuncmap[key](QString(value).toInt());
    }

}


void SettingParser::flushBuf()
{
    if(!changed)
        return;
    QByteArray res=makeSettings();
    qDebug()<<res;
    QFile file(filepath);
    file.open(QIODevice::WriteOnly);
    file.write(Encipher::getInstance()->aesEncrypt(res));
    file.close();
}

bool SettingParser::isFirst()
{
    return settingmap.isEmpty();
}


SettingParser::SettingParser()
{



}
QByteArray SettingParser::newSettings()
{
    QJsonObject rootobj;
    QJsonObject loginobj;

    loginobj.insert("user","");
    loginobj.insert("password","");
    loginobj.insert("remember","0");
    loginobj.insert("autologin","0");

    QJsonObject settingobj;
    settingobj.insert("newmessagering","1");
    settingobj.insert("voicecallring","1");
    settingobj.insert("videocallring","1");
    settingobj.insert("enter","1");
    settingobj.insert("ring","0");

    rootobj.insert("loginsettings",loginobj);
    rootobj.insert("settings",settingobj);

    QJsonDocument doc(rootobj);
    QByteArray json=doc.toJson();
    return json;
}

QByteArray SettingParser::makeSettings()
{
    QJsonObject rootobj;
    QJsonObject loginobj;

    loginobj.insert("user",settingmap["user"]);
    loginobj.insert("password",settingmap["password"]);
    loginobj.insert("remember",settingmap["remember"]);
    loginobj.insert("autologin",settingmap["autologin"]);

    QJsonObject settingobj;
    settingobj.insert("newmessagering",settingmap["newmessagering"]);
    settingobj.insert("voicecallring",settingmap["voicecallring"]);
    settingobj.insert("videocallring",settingmap["videocallring"]);
    settingobj.insert("enter",settingmap["enter"]);
    settingobj.insert("ring",settingmap["ring"]);

    rootobj.insert("loginsettings",loginobj);
    rootobj.insert("settings",settingobj);

    QJsonDocument doc(rootobj);
    QByteArray json=doc.toJson();
    return json;
}

QMap<QString,QString> SettingParser::getMap()
{
    return settingmap;
}


